<template>
    <Dialog
      :show="dialogConfig.show"
      :title="dialogConfig.title"
      :buttons="dialogConfig.buttons"
      :showCancel="true"
      @close="dialogConfig.show = false"
        :width="600+'px'"
    >
     <div style="height: 500px;">
        <el-form
        :model="formData"
        :rules="rules"
        ref="formDataRef"
        label-width="80px"
        @submit.prevent
      >
        <el-form-item label="图书标题" prop="title" label-width="120px">
          <el-input :maxLength="400" v-model="formData.title" />
        </el-form-item>

        <el-form-item label="作者" prop="author"  label-width="120px">
          <el-input  v-model="formData.author" />
        </el-form-item>

        <el-form-item label="分类" prop="category"  label-width="120px">
          <el-input  v-model="formData.category" />
        </el-form-item>
        <el-form-item label="地址" prop="location"  label-width="120px">
          <el-input v-model="formData.location" />
        </el-form-item>

        <el-form-item label="可借数量" prop="quantity"  label-width="120px">
          <el-input v-model="formData.quantity" />
        </el-form-item>

        <el-form-item label="总数量" prop="totalQuantity"  label-width="120px">
          <el-input v-model="formData.totalQuantity" />
        </el-form-item>

        <el-form-item label="允许借阅天数" prop="availableDays" label-width="120px">
          <el-input v-model="formData.availableDays" />
        </el-form-item>
        <el-form-item label="出版社" prop="publishPress" label-width="120px">
          <el-input v-model="formData.publishPress" />
        </el-form-item>

        <el-form-item label="描述" prop="description" label-width="120px">
          <el-input type="textarea" v-model="formData.description" :rows="4" />
        </el-form-item>
      </el-form>
     </div>
    </Dialog>
  </template>

  <script setup>
  import { ref, reactive, getCurrentInstance, nextTick } from "vue";
  const { proxy } = getCurrentInstance();

  const api = {
    saveAccount: "/bookInfo/saveBook",
  };


  const dialogConfig = ref({
    show: false,
    title: "新增图书",
    buttons: [
      {
        type: "primary",
        text: "确定",
        click: (e) => {
          submitForm();
        },
      },
    ],
  });

  const formData = ref({});
  const formDataRef = ref();

  const validateRePass = (rule, value, callback) => {
    if (value !== formData.value.password) {
      callback(new Error(rule.message));
    } else {
      callback();
    }
  };

  const rules = {
    title: [{ required: true, message: "请输入标题" }],
    author: [{ required: true, message: "请输入作者" }],
    category: [{ required: true, message: "请输入分类" }],
    location: [{ required: true, message: "请输入图书地址" }],
    quantity: [{ required: true, message: "请输入可借阅数量" },
    {
        validator: proxy.Verify.number,
        trigger: "blur",
        message: "请输入正确的数字",
      },
    ],
    totalQuantity: [{ required: true, message: "请输入图书总数" },
    {
        validator: proxy.Verify.number,
        trigger: "blur",
        message: "请输入正确的数字",
      },
    ],
    availableDays: [{ required: true, message: "请输入允许借阅天数" },
    {
        validator: proxy.Verify.number,
        trigger: "blur",
        message: "请输入正确的数字",
      },
    ],
    publishPress: [{ required: true, message: "请输入出版社" }],

    phone: [
      { required: true, message: "请输入手机号" },
      {
        validator: proxy.Verify.phone,
        trigger: "blur",
        message: "请输入正确的手机号",
      },
    ],
    email: [
      {
        validator: proxy.Verify.email,
        trigger: "blur",
        message: "请输入正确的邮箱",
      },
    ],
    roleType: [{ required: true, message: "请选择角色" }],
  };

  const showEdit = (data) => {
    dialogConfig.value.show = true;
    nextTick(() => {
      formDataRef.value.resetFields();
      if (data.bookId == null) {
        dialogConfig.value.title = "新增图书";
      } else {
        dialogConfig.value.title = "修改图书信息";
        let editData = Object.assign({}, data);
        formData.value = Object.assign({}, editData);
      }
    });
  };

  defineExpose({
    showEdit,
  });

  const emit = defineEmits(["reload"]);
  const submitForm = async () => {
    formDataRef.value.validate(async (valid) => {
      if (!valid) {
        return;
      }
      let params = {};
      Object.assign(params, formData.value);

      let result = await proxy.Request({
        url: api.saveAccount,
        params,
      });
      if (!result) {
        return;
      }
      dialogConfig.value.show = false;
      proxy.Message.success("保存成功");
      emit("reload");
    });
  };
  </script>

  <style lang="scss" scoped>
  .check-span-item {
    float: left;
    margin-right: 10px;
    line-height: 20px;
  }
  </style>
